package org.lanzier.common.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.lanzier.common.dao.IJdbcDao;
import org.lanzier.common.exception.LanzierException;
import org.lanzier.common.form.RequestDataForm;
import org.lanzier.common.form.ResponseDataForm;
import org.lanzier.common.service.IService;
import org.lanzier.common.utils.Environment;
import org.lanzier.common.utils.MapUtils;
import org.lanzier.common.web.utils.RequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * @author lansb 用户URL统一入口
 */
@Controller
@RequestMapping("/topic")
public class TopicController {
	protected static Logger logger = Logger.getLogger(TopicController.class);

	@Autowired
	private IJdbcDao jdbcDao;

	@RequestMapping(value = "/{id}")
	public String topic(@PathVariable
	String id, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.debug("***************************");
		logger.debug("enter topic");
		logger.debug("***************************");
		String service = null;
		//权限
		
		//查找对应的URL处理参数
		Map<String, Object> urlSqlMap = jdbcDao.queryForMap(
				"select * from sys_url_tab t where t.url_id=?",
				new Object[] { id });
		if (urlSqlMap == null || urlSqlMap.size() == 0) {
			throw new LanzierException(
					"error! no url defined!Please insert a record into the table sys_url_tab.");
		}
		RequestDataForm requestDataForm = RequestUtils.getRequestDataForm(request);
		requestDataForm.setUriId(id);
		requestDataForm.setUrlSqlMap(urlSqlMap);
		service = MapUtils.getString(urlSqlMap, "SERVICE_NAME");
		if (null == service || "".equals(service)) {//service为空，直接跳转到页面
			return MapUtils.getString(urlSqlMap, "PAGE");
		} else {
			request.setAttribute(Environment.URL_SQL_MAP, urlSqlMap);// add
			WebApplicationContext ctx = WebApplicationContextUtils 
					.getWebApplicationContext(request.getSession()
							.getServletContext());
			ResponseDataForm responseDataForm = ((IService) ctx.getBean(service)).service(
					requestDataForm);
			request.setAttribute("responseDataForm", responseDataForm);
			return MapUtils.getString(urlSqlMap, "PAGE");
		}
	}

	@RequestMapping(value = "/ajax/{id}")
	public @ResponseBody
	Object ajax(@PathVariable
	String id, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.debug("***************************");
		logger.debug("enter topic");
		logger.debug("***************************");
		String service = null;
		RequestDataForm requestDataForm = RequestUtils.getRequestDataForm(request);
		Map<String, Object> urlSqlMap = jdbcDao.queryForMap(
				"select * from sys_url_tab t where t.url_id=?",
				new Object[] { id });
		if (urlSqlMap == null || urlSqlMap.size() == 0) {
			throw new LanzierException(
					"Error! No url defined!Please insert into a record to sys_url_tab.");
		}
		service = MapUtils.getString(urlSqlMap, "SERVICE_NAME");
		requestDataForm.setUrlSqlMap(urlSqlMap);
		if (null == service || "".equals(service)) {
			return "undefined service";
		} else {
			WebApplicationContext ctx = WebApplicationContextUtils
					.getWebApplicationContext(request.getSession()
							.getServletContext());
			return ((IService) ctx.getBean(service)).service(requestDataForm).getAjaxObject();
		}
	}
}
